/*
 * @Description: 
 * @Author: 姜暖阳
 * @Date: 2021-07-08 15:10:26
 * @LastEditTime: 2022-01-10 16:34:41
 * @LastEditors: 赵大壮
 * @Reference: 
 */
import axios from 'axios'
import { nyNetErrorType, nyNetError } from './nyNetError'
import nyMessageToast from '../utils/NYMessageToast'

const netError = new nyNetError();

// 创建 axios 实例
const service = axios.create({})

axios.defaults.withCredentials = true
axios.defaults.headers.post['Content-Type'] = 'application/json';
//在main.js设置全局的请求次数，请求的间隙
const err = (error: any) => {
    if(error.message == "Request failed with status code 404") {
        nyMessageToast.error('网络连接失败，请稍后再试');
    }
    if(error.message.indexOf('timeout') > -1) {
        netError.error(nyNetErrorType.timeout, null);
    }

    return Promise.reject(error)
}

// request interceptor
service.interceptors.request.use(config => {
    if (config.method === 'post') {
        
    }
    return config
}, err)

// response interceptor
service.interceptors.response.use((response) => {
    return response.data
}, err)

export default service
